package com.programmercarl.tree;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
 *
 * @author yulongtian
 * @create 2024-12-17 14:57
 */
public class Test04 {
    public static void main(String[] args) {

    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (root == null) {
            return res;
        }
        Queue<Node> q = new LinkedList<Node>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node cur = q.poll();
                assert cur != null;
                temp.add(cur.val);
                if (cur.children != null) {
                    q.addAll(cur.children);
                }
            }
            res.add(temp);
        }

        return res;
    }

}
